package com.alibaba.sdk.config.shiro;

import com.alibaba.sdk.config.ShiroProperties;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 * shiro配置，加载条件为 AuthorizingRealm 类对象是否加载。
 *
 * 请求连接过滤器配置如下。
 * <blockquote><pre>
 *     ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
 *     shiroFilterFactoryBean.setSecurityManager(webSecurityManager);
 * </pre></blockquote><p>
 *
 * 路径配置。
 * <blockquote><pre>
 *     Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
 *     // 不需要登录的路径
 *     filterChainDefinitionMap.put("/login", "anon");
 *     filterChainDefinitionMap.put("/oss/uploadCallback", "anon");
 *     filterChainDefinitionMap.put("/error/**", "anon");
 *     // swagger接口权限 开放
 *     filterChainDefinitionMap.put("/swagger-ui.html", "anon");
 *     filterChainDefinitionMap.put("/webjars/**", "anon");
 *     filterChainDefinitionMap.put("/v2/**", "anon");
 *     filterChainDefinitionMap.put("/swagger-resources/**", "anon");
 *     filterChainDefinitionMap.put("/apiRemote/**", "anon");
 *     // 默认需要登录
 *     filterChainDefinitionMap.put("/**", "authc");
 *     shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
 * </pre></blockquote><p>
 *
 * 需要创建过滤器
 * <blockquote><pre>
 *     Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
 *     // 登录过滤器
 *     filters.put("authc", new ShiroLoginFilter());
 * </pre></blockquote><p>
 *
 * @ writer Shawn(Shawn_0217@163.com)
 * @ date & time 2021/4/27 0:48
 * @ company alibaba
 */
@Configuration
public class ShiroConfig {

    @Resource
    private ShiroProperties shiroProperties;

    /**
     * shiro过滤器实例化
     *
     * @param securityManager
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        // 不需要登录的路径
        filterChainDefinitionMap.put("/login", "anon");
        filterChainDefinitionMap.put("/test/**", "anon");
        filterChainDefinitionMap.put("/error/**", "anon");
        // swagger接口权限 开放
        filterChainDefinitionMap.put("/swagger-ui.html", "anon");
        filterChainDefinitionMap.put("/webjars/**", "anon");
        filterChainDefinitionMap.put("/v2/**", "anon");
        filterChainDefinitionMap.put("/swagger-resources/**", "anon");
        filterChainDefinitionMap.put("/apiRemote/**", "anon");
        // 默认需要登录
        filterChainDefinitionMap.put("/**", "authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
        // 登录过滤器
        filters.put("authc", new ShiroLoginFilter());
        return shiroFilterFactoryBean;
    }

    /**
     * 用户信息管理存储实例化
     *
     * @param shiroRealm   用户身份信息校验
     * @return SecurityManager 用户加密信息管理对象
     */
    @Bean
    public DefaultWebSecurityManager securityManager(ShiroRealm shiroRealm, CookieRememberMeManager rememberMeManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(shiroRealm);
        securityManager.setCacheManager(new EhCacheManager());
        securityManager.setRememberMeManager(rememberMeManager);
        securityManager.setSessionManager(new DefaultWebSessionManager());
        return securityManager;
    }

    /**
     * cookie管理对象
     *
     * @return
     */
    @Bean
    public CookieRememberMeManager rememberMeManager() {
        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
        // rememberMe cookie加密的密钥
        cookieRememberMeManager.setCipherKey(Base64.decode("4AvVhmFLUs0KTA3Kprsdag=="));

        // 设置cookie名称，设置cookie的过期时间(单位秒)
        SimpleCookie cookie = new SimpleCookie("rememberMe");
        cookie.setMaxAge(shiroProperties.getRememberMeTimeout());
        cookieRememberMeManager.setCookie(cookie);
        return cookieRememberMeManager;
    }

    /**
     * 开启shiro aop注解支持.
     * 使用代理方式;所以需要开启代码支持;
     *
     * @param securityManager
     * @return AuthorizationAttributeSourceAdvisor
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
